package com.github.tzraeq.d2d;

import android.support.annotation.StringDef;
import android.view.View;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.events.RCTEventEmitter;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class PlayerEventEmitter {

    private final RCTEventEmitter eventEmitter;

    private int viewId = View.NO_ID;

    PlayerEventEmitter(ReactContext reactContext) {
        this.eventEmitter = reactContext.getJSModule(RCTEventEmitter.class);
    }

    private static final String EVENT_ON_PLAYING = "onPlaying";
    private static final String EVENT_ON_STOPPED = "onStopped";
    private static final String EVENT_ON_VOUT = "onVout";
    private static final String EVENT_ON_SCALE = "onScale";

    static final String[] Events = {
            EVENT_ON_PLAYING,
            EVENT_ON_STOPPED,
            EVENT_ON_VOUT,
            EVENT_ON_SCALE,
    };

    @Retention(RetentionPolicy.SOURCE)
    @StringDef({
            EVENT_ON_PLAYING,
            EVENT_ON_STOPPED,
            EVENT_ON_VOUT,
            EVENT_ON_SCALE,
    })

    @interface VideoEvents {}

    void setViewId(int viewId) {
        this.viewId = viewId;
    }

    public void onPlaying(WritableMap map){
        receiveEvent(EVENT_ON_PLAYING, map);
    }

    public void onVout(WritableMap map){
        receiveEvent(EVENT_ON_VOUT, map);
    }

    public void onStopped(WritableMap map){
        receiveEvent(EVENT_ON_STOPPED, map);
    }

    public void onScale(WritableMap map){
        receiveEvent(EVENT_ON_SCALE, map);
    }

    private void receiveEvent(@VideoEvents String type, WritableMap event) {
        eventEmitter.receiveEvent(viewId, type, event);
    }
}
